package com.example.libraryapp.user;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.example.libraryapp.book.BookInfo;
import com.example.libraryapp.db.MySqliteHelper;
import com.example.libraryapp.util.Constance;

/**
 * 主要提供对用户信息的管理
 */
public class UserService {
	/**
	 * 用户注册
	 * 
	 * @param username
	 * @param password
	 */
	public static boolean register(Context context, UserInfo userInfo) {
		ContentValues values = new ContentValues();
		values.put("cardid", userInfo.getCardid());
		values.put("username", userInfo.getUsername());
		values.put("password", userInfo.getPassword());
		long registerResult = MySqliteHelper.getInstance(context).insert(
				Constance.USER_TABLE_NMAE, null, values);
		if (registerResult != -1) {
			return true;
		}
		return false;
	}

	/**
	 * 用户登录
	 * 
	 * @param context
	 * @param username
	 * @param password
	 * @return
	 */
	public static UserInfo login(Context context, String cardid,
			String password) {
		UserInfo userInfo = null;
		String where = "cardid=" + cardid;
		where += " and ";
		where += "password=" + password;
		Cursor cursor = MySqliteHelper.getInstance(context).query(Constance.USER_TABLE_NMAE, where);
		if (cursor != null) {
			while(cursor.moveToNext()){
				userInfo = new UserInfo();
				userInfo.setUrid(cursor.getInt(cursor.getColumnIndex("urid")));
				userInfo.setCardid(cursor.getString(cursor.getColumnIndex("cardid")));
				userInfo.setUsername(cursor.getString(cursor.getColumnIndex("username")));
			}
		}
		return userInfo;
	}
	
	/**
	 * 删除表
	 * @param context
	 */
	public static void clear(Context context) {
		MySqliteHelper.getInstance(context).clearCache(Constance.USER_TABLE_NMAE);
	}
}
